package com.cjg.Class;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class demo09 {
    //正常方式创建
    public  static void test01(){
        Student student = new Student();
        long l = System.currentTimeMillis();
        for (int i = 0; i < 100000000; i++) {
            student.getAge();
        }
        long l1 = System.currentTimeMillis();
        System.out.println(l1-l);
    }
    //反射创建
 public static void test02() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Student student = new Student();
     Class<? extends Student> aClass = student.getClass();
     Method getAge = aClass.getDeclaredMethod("getAge");
     long l = System.currentTimeMillis();
        for (int i = 0; i < 100000000; i++) {
           getAge.invoke(student);
        }
        long l1 = System.currentTimeMillis();
        System.out.println(l1-l);
    }
    //关闭检查 创建
    public static void test03() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Student student = new Student();
     Class<? extends Student> aClass = student.getClass();
     Method getAge = aClass.getDeclaredMethod("getAge");
     getAge.setAccessible(true);
     long l = System.currentTimeMillis();
        for (int i = 0; i < 100000000; i++) {


            getAge.invoke(student);
        }
        long l1 = System.currentTimeMillis();
        System.out.println(l1-l);
    }

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        test01();
        test02();
        test03();
    }

}
